package com.ocom.mqsimple.business.unicom;

import com.alibaba.fastjson.JSON;
import com.ocom.common.entity.card.EntityXfAccountInfo;
import com.ocom.common.request.company.GetComRequestRequest;
import com.ocom.common.request.web.CommonPidVo;
import com.ocom.common.resp.Result;
import com.ocom.common.response.GetComRequestResponse;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.vo.human.PersonBornVo;
import com.ocom.mqsimple.config.UnicomAccTypeMqConfig;
import com.ocom.mqsimple.feign.CompanyClientService;
import com.ocom.mqsimple.feign.HumanClientService;
import com.ocom.mqsimple.feign.PayConfigClientService;
import com.ocom.mqsimple.vo.UnicomParamVo;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;


@Component
public class UnicomAccTypeConsumerMqListener {

    private Logger logger = LoggerFactory.getLogger(UnicomAccTypeConsumerMqListener.class);

    @Autowired
    CompanyClientService companyClientService;

    @Autowired
    PayConfigClientService payConfigClientService;

    @Autowired
    HumanClientService humanClientService;


    @RabbitListener(queues = UnicomAccTypeMqConfig.UNICOM_QUEUE)
    public void process(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {
        logger.info("message【{}】", message);
        String requestMessage = new String(message.getBody(), StandardCharsets.UTF_8);


        try {
            CommonPidVo request = JSON.parseObject(requestMessage, CommonPidVo.class);

            logger.info("执行中 【{}】", request);
            GetComRequestRequest getComRequestRequest = new GetComRequestRequest();
            getComRequestRequest.setComId(request.getComId());
            getComRequestRequest.setDockCode("unicom");
            getComRequestRequest.setDockApiCode("unicom#updateAccType");
            Result result = companyClientService.getComRequest(getComRequestRequest);
            if (result.getCode() == 200) {
                GetComRequestResponse response = JSON.parseObject(JSON.toJSONString(result.getData()), GetComRequestResponse.class);
                System.out.println(response);
                if (!CommonUtil.isNull(response.getRsa2Pri())) {
                    UnicomParamVo unicomParamVos = JSON.parseObject(response.getRsa2Pri(), UnicomParamVo.class);
                    for (UnicomParamVo.UnicomParam unicomParamVo : unicomParamVos.getAcctypeUpgrade()) {
                        LocalDate currentDate = LocalDate.now();
                        LocalDate tenYearsAgo = currentDate.minusYears(unicomParamVo.getYears());
                        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                        String formattedDate = tenYearsAgo.format(formatter);

                        Result result1 = humanClientService.getPersonAccountByBorn(request.getComId(), unicomParamVo.getOldType(), formattedDate);
                        if (result1.getCode() == 200 && result1.getData() != null) {

                            List<PersonBornVo> personBornVos = JSON.parseArray(JSON.toJSONString(result1.getData()), PersonBornVo.class);
                            if (personBornVos != null && personBornVos.size() > 0) {
                                for (PersonBornVo personBornVo : personBornVos) {
                                    EntityXfAccountInfo entityXfAccountInfo = new EntityXfAccountInfo();
                                    entityXfAccountInfo.setId(personBornVo.getAccId());
                                    entityXfAccountInfo.setComId(request.getComId());
                                    entityXfAccountInfo.setAccTypeId(unicomParamVo.getNewType());
                                    entityXfAccountInfo.setPId(personBornVo.getPId());
                                    entityXfAccountInfo.setAccountState(personBornVo.getAccountState());
                                    payConfigClientService.UpdateXFAccount(entityXfAccountInfo);
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("UnicomAccType-出现异常队列消息开始进入死信队列【{}】 【{}】",requestMessage, e);
        }

        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }


}
